#include "mainwindow.h"
#include <QDir>
#include <QProxyStyle>

class CustomStyle : public QProxyStyle {
public:
    int pixelMetric(PixelMetric metric, const QStyleOption *option, const QWidget *widget) const override {
        if (metric == QStyle::PM_SmallIconSize) {
            return 30; // 设置图标大小为24像素
        }
        return QProxyStyle::pixelMetric(metric, option, widget);
    }
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{

    this->setWindowIcon(QIcon(":/img/icon2.ioc"));

    tabWidget = new QTabWidget;
    tabWidget->tabBar()->setTabsClosable(true);

    tabManager = new TabManager(this);

    terminal = new Terminal(this, tabManager);
    terminal->setStyleSheet("background: black; color: white;");
    terminal->hide();

    // 创建一个 QSplitter
    QSplitter *splitter = new QSplitter(Qt::Vertical);


    splitter->addWidget(tabWidget);
    splitter->addWidget(terminal);

    this->setMinimumSize(QSize(800,600));
    this->resize(1920, 1080);
    //this->setCentralWidget(tabWidget);
    setCentralWidget(splitter);
    QList<int> sizes;
    sizes << 300 << 100;
    splitter->setSizes(sizes);


    file = this->menuBar()->addMenu("文件");
    edit = this->menuBar()->addMenu("编辑");
    help = this->menuBar()->addMenu("帮助");
    mTerminal = this->menuBar()->addMenu("终端");
    build = this->menuBar()->addMenu("运行");
    build->setEnabled(false);
    tools = this->menuBar()->addMenu("工具");
    QFont fontMenu = menuBar()->font();
    fontMenu.setPixelSize(25); // 设置字体大小
    menuBar()->setFont(fontMenu);
    CustomStyle *style = new CustomStyle();
    menuBar()->setStyle(style);
    //初始化上方菜单栏

    file_new = new QAction("新建",this);
    file_new->setShortcut(tr("ctrl+n"));
    file_new->setFont(fontMenu);
    file_open = new QAction("打开",this);
    file_open->setShortcut(tr("ctrl+o"));
    file_open->setFont(fontMenu);
    file_exit = new QAction("退出",this);
    file_exit->setShortcut(tr("ctrl+q"));
    file_exit->setFont(fontMenu);
    file_save = new QAction("保存",this);
    file_save->setShortcut(tr("ctrl+s"));
    file_save->setFont(fontMenu);
    file_save_as = new QAction("另存为",this);
    file_save_as->setFont(fontMenu);
    edit_copy = new QAction("复制",this);
    edit_copy->setShortcut(tr("ctrl+c"));
    edit_copy->setFont(fontMenu);
    edit_paste = new QAction("粘贴",this);
    edit_paste->setShortcut(tr("ctrl+v"));
    edit_paste->setFont(fontMenu);
    edit_cut = new QAction("剪切",this);
    edit_cut->setShortcut(tr("ctrl+x"));
    edit_cut->setFont(fontMenu);
    edit_selectAll = new QAction("全选",this);
    edit_selectAll->setShortcut(tr("ctrl+a"));
    edit_selectAll->setFont(fontMenu);
    help_instruction = new QAction("用户手册", this);
    help_instruction->setFont(fontMenu);
    build_compile = new QAction("编译",this);
    build_compile->setFont(fontMenu);
    build_run = new QAction("运行",this);
    build_run->setFont(fontMenu);
    build_compileAndRun = new QAction("编译并运行",this);
    build_compileAndRun->setFont(fontMenu);
    find_and_replace = new QAction("查找替换",this);
    find_and_replace->setShortcut(tr("ctrl+f"));
    find_and_replace->setFont(fontMenu);
    back = new QAction(QIcon("./resource/back.png"),"撤销",this);
    back->setShortcut(tr("ctrl+z"));
    back->setFont(fontMenu);
    forward = new QAction(QIcon("./resource/forward.png"),"前进",this);
    forward->setFont(fontMenu);
    this->menuBar()->addAction(back);
    this->menuBar()->addAction(forward);
    forward->setEnabled(false);
    back->setEnabled(false);
    showTerminal = new QAction("显示终端", this);
    showTerminal->setFont(fontMenu);
    hideTerminal = new QAction("隐藏终端", this);
    hideTerminal->setFont(fontMenu);
    tools_compiler_option = new QAction("编译设置");
    tools_compiler_option->setFont(fontMenu);
    tools_settings = new QAction("编辑器选项");
    tools_settings->setFont(fontMenu);
    //初始化菜单栏中的按钮部件

    file_save->setEnabled(false);
    file_save_as->setEnabled(false);

    file->addAction(file_new);
    file->addAction(file_open);
    file->addAction(file_save);
    file->addAction(file_save_as);
    file->addSeparator();
    file->addAction(file_exit);
    edit->addAction(edit_copy);
    edit->addAction(edit_paste);
    edit->addAction(edit_cut);
    edit->addAction(edit_selectAll);
    edit->addSeparator();
    edit->addAction(find_and_replace);
    help->addAction(help_instruction);
    build->addAction(build_compileAndRun);
    build->addSeparator();
    build->addAction(build_compile);
    build->addAction(build_run);
    mTerminal->addAction(showTerminal);
    mTerminal->addAction(hideTerminal);
    tools->addAction(tools_compiler_option);
    tools->addAction(tools_settings);
    //在菜单下排放按钮部件

    find_layout = new QHBoxLayout;
    find_widget = new QWidget;
    find_widget->setLayout(find_layout);

    find_layout->addWidget(tabManager->textEditor->find_input);
    find_layout->addWidget(tabManager->textEditor->replace_button);
    find_layout->addWidget(tabManager->textEditor->start_button);
    find_layout->addWidget(tabManager->textEditor->next_button);
    find_layout->addWidget(tabManager->textEditor->cancel_button);
    find_layout->addWidget(tabManager->textEditor->spacer);

    toolbar = new QToolBar("Main Toolbar", this);
    toolbar->setIconSize(QSize(45, 45));
    addToolBar(Qt::LeftToolBarArea, toolbar); // 将工具栏停靠在左侧
    // 创建动作并设置自定义图标
    //QString currentPath = QDir::currentPath();
    //qDebug() << "Current working directory:" << currentPath;
    // 工具栏
    // 新建
    QAction *newAction = new QAction(QIcon("./resource/new.png"), "new", this);
    toolbar->addAction(newAction);
    connect(newAction,SIGNAL(triggered()),tabManager,SLOT(on_newfile()));
    //打开
    QAction *openAction = new QAction(QIcon("./resource/open.png"), "open", this);
    toolbar->addAction(openAction);
    connect(openAction,SIGNAL(triggered()),tabManager,SLOT(on_open()));
    //终端
    QAction *terminalAction = new QAction(QIcon("./resource/terminal.png"), "terminal", this);
    toolbar->addAction(terminalAction);
    connect(terminalAction,SIGNAL(triggered()),terminal,SLOT(toggleTerminal()));
    toolbar->addSeparator();
    //编译运行
    buildAndRunAction = new QAction(QIcon("./resource/buildAndRun.png"), "buildAndRun", this);
    toolbar->addAction(buildAndRunAction);
    connect(buildAndRunAction,SIGNAL(triggered()),terminal,SLOT(onBuildAndRun()));
    buildAndRunAction->setEnabled(false);
    //编译
    buildAction = new QAction(QIcon("./resource/build.png"), "build", this);
    toolbar->addAction(buildAction);
    connect(buildAction,SIGNAL(triggered()),terminal,SLOT(onBuild()));
    toolbar->addSeparator();
    buildAction->setEnabled(false);
    //设置
    QAction *settingAction = new QAction(QIcon("./resource/setting.png"), "setting", this);
    toolbar->addAction(settingAction);
    connect(settingAction,SIGNAL(triggered()),tabManager->toolsSettings, SLOT(openToolsSettings()));

    find_toolbar = addToolBar("查找");
    find_toolbar->addWidget(find_widget);
    find_widget->setVisible(false);
    find_toolbar->setVisible(false);
    find_toolbar->setFont(fontMenu);
    //"查找“栏的初始化

    replace_layout = new QHBoxLayout;
    replace_widget = new QWidget;
    replace_widget->setLayout(replace_layout);

    replace_layout->addWidget(tabManager->textEditor->replace_input);
    replace_layout->addWidget(tabManager->textEditor->replace_all_button);
    replace_layout->addWidget(tabManager->textEditor->replace_select_button);
    replace_layout->addWidget(tabManager->textEditor->replace_cancel_button);
    replace_layout->addWidget(tabManager->textEditor->spacer_);

    replace_toolbar = addToolBar("替换");
    replace_toolbar->addWidget(replace_widget);
    replace_widget->setVisible(false);
    replace_toolbar->setVisible(false);
    replace_toolbar->setFont(fontMenu);
    //"替换“栏的初始化

    connect(file_new,SIGNAL(triggered()),tabManager,SLOT(on_newfile()));
    connect(file_open,SIGNAL(triggered()),tabManager,SLOT(on_open()));
    connect(file_exit,SIGNAL(triggered()),tabManager,SLOT(on_exit()));
    connect(file_save,SIGNAL(triggered()),tabManager,SLOT(on_save()));
    connect(file_save_as,SIGNAL(triggered()),tabManager,SLOT(on_save_as()));
    connect(tabWidget, SIGNAL(currentChanged(int)),tabManager,SLOT(on_tab_changed(int)));
    connect(tabWidget->tabBar(), SIGNAL(tabCloseRequested(int)),tabManager, SLOT(on_close_tab(int)));
    connect(edit_copy,SIGNAL(triggered()),tabManager->textEditor,SLOT(on_copy()));
    connect(edit_paste,SIGNAL(triggered()),tabManager->textEditor,SLOT(on_paste()));
    connect(edit_cut,SIGNAL(triggered()),tabManager->textEditor,SLOT(on_cut()));
    connect(edit_selectAll,SIGNAL(triggered()),tabManager->textEditor,SLOT(on_selectAll()));
    connect(find_and_replace,SIGNAL(triggered()),tabManager->textEditor,SLOT(on_find_and_replace()));
    connect(build_compile,SIGNAL(triggered()),terminal,SLOT(onBuild()));
    connect(build_run,SIGNAL(triggered()),terminal,SLOT(onRun()));
    connect(build_compileAndRun,SIGNAL(triggered()),terminal,SLOT(onBuildAndRun()));
    connect(back,SIGNAL(triggered()),tabManager->backForward,SLOT(on_back()));
    connect(forward,SIGNAL(triggered()),tabManager->backForward,SLOT(on_forward()));
    connect(showTerminal, SIGNAL(triggered()), terminal, SLOT(showTerminal()));
    connect(hideTerminal, SIGNAL(triggered()), terminal, SLOT(hideTerminal()));
    connect(tools_compiler_option, SIGNAL(triggered()), tabManager->compilerOption, SLOT(openCompilerOptions()));
    connect(tools_settings, SIGNAL(triggered()), tabManager->toolsSettings, SLOT(openToolsSettings()));
    connect(help_instruction, SIGNAL(triggered()), tabManager, SLOT(onOpenDoc()));
    //将按钮部件与功能函数进行连接

}

MainWindow::~MainWindow()
{
    delete file_new;
    delete file_open;
    delete file_exit;
    delete file_save;
    delete file_save_as;
    delete edit_copy;
    delete edit_paste;
    delete edit_cut;
    delete edit_selectAll;
    delete build_compile;
    delete build_run;
    delete build_compileAndRun;
    delete find_and_replace;
    delete back;
    delete forward;

    delete file;
    delete edit;
    delete help;
    delete tabManager;
    delete mTerminal;
    delete build;
}


